#include "rwxml.h"
#include "ui_rwxml.h"

RWXml::RWXml(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::RWXml)
{
    ui->setupUi(this);
}

RWXml::~RWXml()
{
    delete ui;
}

#include <QFileDialog>
#include <QXmlStreamReader>
#include <QXmlStreamWriter>

//读xml
void RWXml::on_pushButton_clicked()
{
    auto diag = new QFileDialog(this);
    diag->setWindowTitle("选择要读取的xml文件");
    diag->setFileMode(QFileDialog::AnyFile);
    diag->setAcceptMode(QFileDialog::AcceptMode::AcceptOpen);
    diag->setNameFilter(tr("xml文件(*.xml)"));
    diag->setDirectory(".");
    if(diag->exec()== QDialog::Accepted)
    {
        this->ui->tb_info->clear();
        auto f = diag->selectedFiles();
        for(auto i=f.begin();i!=f.end();i++)
            this->ui->tb_info->append(*i);
        auto reader = new QXmlStreamReader();
        QFile file(*f.begin());
        file.open(QFile::ReadOnly|QFile::Text);
        reader->setDevice(&file);
        reader->readNext();
        int c=0;
        while (!reader->atEnd())
        {
            if(reader->isStartElement())
            {
                std::string s = std::string(c*2,' ');
                this->ui->tb_info->append((s+reader->name().toString().toStdString()+"【Start】").c_str());
                c++;
                auto attrs = reader->attributes();
                for(auto att = attrs.begin();att!=attrs.end();att++)
                {
                    this->ui->tb_info->append( "【ATT】"+att->name() +" = "+att->value());
                }
                this->ui->tb_info->append( "【TXT】"+reader->readElementText()); //访问当前节点的内容
            }
            if(reader->isEndElement())
            {
                c--;
                std::string s = std::string(c*2,' ');
                this->ui->tb_info->append((s+reader->name().toString().toStdString()+"【End】").c_str());
            }
            if(reader->isCDATA())
            {
                std::string txt = "CDATA "+reader->text().toString().toStdString();
                this->ui->tb_info->append(txt.c_str());
            }
            if(reader->isDTD())
            {
                std::string txt = "DTD "+reader->text().toString().toStdString();
                this->ui->tb_info->append(txt.c_str());
            }
            reader->readNext();
        }
        file.close();
        delete reader;
    }
}

//写xml
void RWXml::on_pushButton_2_clicked()
{
    auto diag = new QFileDialog(this);
    diag->setWindowTitle("选择要保存的xml");
    diag->setAcceptMode(QFileDialog::AcceptMode::AcceptSave);
    diag->setNameFilter(tr("xml文件(*.xml)"));
    diag->setDirectory(".");
    if(diag->exec()!= QDialog::Accepted)
    {
        delete diag;
        return;
    }
    auto f = *diag->selectedFiles().begin();
    QFile file(f);
    file.open(QFile::NewOnly|QFile::WriteOnly|QFile::Text);
    QXmlStreamWriter writer;
    writer.setDevice(&file);
    writer.setAutoFormatting(true);

    writer.writeStartDocument();

    writer.writeStartElement("doc");
    writer.writeTextElement("txt","content");
    writer.writeStartElement("data");
    writer.writeAttribute("xml","true");
    writer.writeAttribute("json","true");
    writer.writeComment("Comment");
    writer.writeCDATA("【CDATA】");
    writer.writeDTD("【DTD】");
    writer.writeCharacters("【Characters】");

    writer.writeEndElement();
    writer.writeEndElement();
    writer.writeEndDocument();

    file.flush();
    file.close();

    delete diag;
}

#include <QDomDocument>
//dom 读xml
void RWXml::on_pushButton_R_DOM_clicked()
{
    auto diag = new QFileDialog(this);
    diag->setWindowTitle("选择要读取的xml文件");
    diag->setFileMode(QFileDialog::AnyFile);
    diag->setAcceptMode(QFileDialog::AcceptMode::AcceptOpen);
    diag->setNameFilter(tr("xml文件(*.xml)"));
    diag->setDirectory(".");
    if(diag->exec()!= QDialog::Accepted)
        return;
    this->ui->tb_info->clear();
    auto f = diag->selectedFiles();
    for(auto i=f.begin();i!=f.end();i++)
        this->ui->tb_info->append(*i);
    QFile file(*f.begin());
    file.open(QFile::ReadOnly|QFile::Text);
    QDomDocument doc;
    if(doc.setContent(&file))
    {
        QDomElement root = doc.documentElement();
        QDomNode first = root.firstChild();
        while (!first.isNull())
        {
            QDomElement e = first.toElement();
            if(!e.isNull())
            {
                this->ui->tb_info->append(e.tagName());
            }
            first = first.nextSibling();
        }
    }
    file.close();
}

#include <QTextStream>
//dom 写xml
void RWXml::on_pushButton_W_DOM_clicked()
{
    auto diag = new QFileDialog(this);
    diag->setWindowTitle("选择要保存的xml");
    diag->setAcceptMode(QFileDialog::AcceptMode::AcceptSave);
    diag->setNameFilter(tr("xml文件(*.xml)"));
    diag->setDirectory(".");
    if(diag->exec()!= QDialog::Accepted)
    {
        delete diag;
        return;
    }
    auto f = *diag->selectedFiles().begin();
    QFile file(f);
    file.open(QFile::NewOnly|QFile::WriteOnly|QFile::Text);
    QDomDocument doc;

    QTextStream out(&file);
    QDomNode n = doc.createProcessingInstruction("xml","version='1.0'");
    doc.insertBefore(n,doc.firstChild());
    doc.appendChild(doc.createComment("comment"));
    auto root = doc.createElement("root");
    doc.appendChild(root);
    root.appendChild(doc.createTextNode("textNode1"));
    root.appendChild(doc.createTextNode("textNode2"));
    root.appendChild(doc.createAttribute("Attribute"));
    doc.save(out,4);
    file.close();
}
